Bee.form.TreeCombo = Ext.extend(Ext.form.ComboBox, {
    displayValue: undefined,
    displayField: 'text',
    valueField: 'id',
    rootText: '根节点',
    rootId: '-1',
    height: 200,
    leaf: false,
    editable: false,
    resizable: true,
    onLoad: function(){
    },
    findRecord: function(){
    },
    initList: function(){
        this.list = new Ext.tree.TreePanel({
            animate: false,
            containerScroll: true,
            autoScroll: true,
            root: new Ext.tree.AsyncTreeNode({
                expandable: true,
                expanded: true,
                text: this.rootText,
                id: this.rootId
            }),
            loader: new Bee.tree.DWRTreeLoader({
                dwrCall: this.dwrCall,
                listeners: this.loaderListeners,
                nodeMeta: {
                    id: this.valueField,
                    text: this.displayField,
                    leaf: this.leaf
                }
            }),
            floating: true,
            height: this.height,
            //autoHeight: true,
            listeners: {
                click: this.onNodeClick,
                beforeclick: this.onBeforeclick ||
                function(){
                },
                collapsenode: function(n){
                    this.list.el.sync(true);
                },
                expandnode: function(n){
                    this.list.el.sync(true);
                },
                scope: this
            },
            alignTo: function(el, pos){
                this.setPagePosition(this.el.getAlignToXY(el, pos));
            }
        });
    },
    expand: function(){
        if (!this.list.rendered) {
            this.list.render(document.body);
            this.innerList = this.list.body;
            //this.store = new Ext.data.Store();
            this.list.setWidth(this.listWidth || this.el.getWidth());
            this.list.hide();
            if (this.resizable) {
                this.resizer = new Ext.Resizable(this.list.el, {
                    minWidth: this.width,
                    pinned: true,
                    handles: 'e'
                });
                this.mon(this.resizer, 'resize', function(r, w, h){
                    this.listWidth = w;
                    this.list.setWidth(this.listWidth - this.list.getFrameWidth('lr'));
                }, this);
            }
        }
        this.el.blur();
        Bee.form.TreeCombo.superclass.expand.apply(this, arguments);
    },
    doQuery: function(q, forceAll){
        this.expand();
    },
    collapseIf: function(e){
        if (!e.within(this.wrap) && !e.within(this.list.el)) {
            this.collapse();
        }
    },
    onNodeClick: function(node, e){
        this.setRawValue(node.attributes.text);
        if (this.hiddenField) {
            this.hiddenField.value = node.id;
        }
        this.collapse();
    }
});

Ext.reg('treecombo', Bee.form.TreeCombo);
